home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / niftp / hdr_proc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  9.9 KB  |  372 lines

  1. #include "util.h"
  2. #include "mmdf.h"
  3. #include "hdr.h"
  4. #include "ch.h"
  5. #include "ap.h"
  6. /*
  7.  *     MULTI-CHANNEL MEMO DISTRIBUTION FACILITY  (MMDF)
  8.  *
  9.  *
  10.  *     Copyright (C) 1979,1980,1981  University of Delaware
  11.  *
  12.  *     Department of Electrical Engineering
  13.  *     University of Delaware
  14.  *     Newark, Delaware  19711
  15.  *
  16.  *     Phone:  (302) 738-1163
  17.  *
  18.  *
  19.  *     This program module was developed as part of the University
  20.  *     of Delaware's Multi-Channel Memo Distribution Facility (MMDF).
  21.  *
  22.  *     Acquisition, use, and distribution of this module and its listings
  23.  *     are subject restricted to the terms of a license agreement.
  24.  *     Documents describing systems using this module must cite its source.
  25.  *
  26.  *     The above statements must be retained with all copies of this
  27.  *     program and may not be removed without the consent of the
  28.  *     University of Delaware.
  29.  *
  30.  *
  31.  *     version  -1    David H. Crocker    March   1979
  32.  *     version   0    David H. Crocker    April   1980
  33.  *     version  v7    David H. Crocker    May     1981
  34.  *     version   1    David H. Crocker    October 1981
  35.  *
  36.  */
  37. /* This module has bits extracted from submit modules                   */
  38. /* It is used by the Q NIFTP channel to extract a sender address        */
  39. /* From the text                                                        */
  40. /* This is really a pretty ugly way to do things - but I didnt write    */
  41. /* the protocol                                                         */
  42. /*                                                                      */
  43. /*      Steve Kille             August 1982                             */
  44. /*      Steve Kille             Feb 83          extensive rehack        */
  45.  
  46.  
  47. extern struct ll_struct *logptr;
  48.  
  49. extern Chan *curchan;
  50.  
  51. extern char *index ();
  52. extern char *compress ();
  53. extern char *ap_p2s();
  54. extern char *multcat();
  55. extern AP_ptr ap_new();
  56.  
  57. extern char *locfullmachine;
  58. extern char *locfullname;
  59. /*
  60. */
  61.  
  62.  
  63. LOCFUN AP_ptr newdomain (domn)
  64.     register char *domn;
  65. {
  66.     return (ap_new ((*domn == '[')? APV_DLIT : APV_DOMN, domn));
  67. }
  68.  
  69.                 /* This is used to get the sender name  */
  70.                 /* Passes over JNT header and then      */
  71.                 /* examines the 822 header fields       */
  72.                 /* Takes Sender field if it exists      */
  73.                 /* If not takes first From field        */
  74. hdr_sender (sender, ack, hdr_fp, viahost)
  75. char    *sender;                /* Where to stuff name of sender        */
  76. char    **ack;                  /* ack address - note double indirection*/
  77. FILE    *hdr_fp;                /* File being examined                  */
  78. char    *viahost;               /* Host message receoved from           */
  79. {
  80.     char        line[LINESIZE];         /* temp buffer                  */
  81.     char        name[LINESIZE];         /* Name of header field         */
  82.     char        contents[LINESIZE];     /* Contents of header field     */
  83.     int         gotfrom;                /* Got address from From        */
  84.     int         len;
  85.     int         donevia;
  86.     char        *p;
  87.     AP_ptr      ap_ack,
  88.         ap_sender,
  89.         jntroute,
  90.         ap,
  91.         domain,
  92.         route,
  93.         mbox;
  94.  
  95. #ifdef DEBUG
  96.    ll_log (logptr, LLOGBTR, "hdr_sender (%s)", viahost);
  97. #endif
  98.  
  99.     gotfrom = FALSE;
  100.     ap_sender = (AP_ptr) 0;
  101.     sender [0] = '\0';
  102.     jntroute = (AP_ptr) 0;
  103.  
  104.                 /* We are at start of file              */
  105.                 /* Therefore skip JNT mail header       */
  106.     while ((len = gcread (hdr_fp, line, LINESIZE, "\n\377")) > 1)
  107.     {
  108.     line [len - 1] = '\0';
  109.     for (p = line ; isspace(*p) ;p++);
  110.     if (*p == '\0')
  111.         break;
  112.     }
  113.     if (len == NOTOK)
  114.     {
  115.     ll_err (logptr, LLOGFAT, "Can't find 733 header");
  116.     return (RP_NO);
  117.     }
  118.  
  119.  
  120.  
  121.                 /* Now process header lines             */
  122.    FOREVER
  123.    {
  124.                 /* Get a line                           */
  125.     if ((len = gcread (hdr_fp, line, LINESIZE, "\n\377")) < 1)
  126.     {
  127.         ll_log (logptr, LLOGTMP, "Message has no body" );
  128.                 /* someone MIGHT want to do this so     */
  129.                 /* just log and pass on through         */
  130.         if (gotfrom)        /* drop down and return adr             */
  131.         break;
  132.         return (RP_NO);
  133.     }
  134.     line [len] = '\0';
  135.     ll_log (logptr, LLOGFTR, "gcread (in header) = '%s' (%d)", line, len);
  136.  
  137.     switch (hdr_parse (line, name, contents))
  138.     {
  139.         case        HDR_NAM:
  140.                 /* No name so contine through header    */
  141.         continue;
  142.  
  143.         case        HDR_EOH:
  144.                 /* End of header                        */
  145.         if (gotfrom)    /* if we have From address pass it back */
  146.                break;
  147.         return (RP_NO);
  148.  
  149.         case        HDR_NEW:
  150.         case        HDR_MOR:
  151.                 /* Real field so check it               */
  152.         if (lexequ (name, "Via"))
  153.         {
  154.             if ((p = index (contents, ';')) == 0)
  155.             {
  156.             ll_log (logptr, LLOGTMP, "Illegal via: field '%s'",
  157.                 contents);
  158.             continue;
  159.             }
  160.             *p = '\0';
  161.             compress (contents, contents);
  162.             /*
  163.              * must take comments out of via field, ( to get info
  164.              * correct )
  165.              * ( delete RFC822 comments from via field )
  166.              */
  167.             while ((p = index (contents, '(')) != NULL)
  168.             {
  169.             char    *q;
  170.             if ((q = index(p, ')')) == NULL)
  171.             {
  172.                  ll_log(logptr, LLOGTMP,
  173.                 "Illegal comment in via: field '%s'", contents);
  174.                  break;
  175.             }
  176.             strcpy(p, q+1);         /* zap () comment */
  177.             }
  178.             if(p != NULL)               /* only non null if no ')' */
  179.             continue;
  180.             compress (contents, contents);
  181. #ifdef DEBUG
  182.             ll_log (logptr, LLOGFTR, "%s: %s", name, contents);
  183. #endif
  184.             ap = newdomain (contents);
  185.             ap -> ap_chain = jntroute;
  186.             jntroute = ap;
  187.         }
  188.  
  189.         if (lexequ (name, "acknowledge-to"))
  190.             if (*ack == (char *) 0)
  191.             {
  192.             ap_ack = ap_s2tree (contents);
  193.             if (ap_ack != (AP_ptr) NOTOK)
  194.             {
  195.                 if(ap_t2s (ap_ack, ack) == (AP_ptr)MAYBE)
  196.                 return(RP_NS);
  197. #ifdef DEBUG
  198.                 ll_log (logptr, LLOGFTR, "Ack field '%s", &ack);
  199. #endif
  200.                 ap_sqdelete (ap_ack, (AP_ptr) 0);
  201.             }
  202.             }
  203.  
  204.         if (lexequ (name, "Sender"))
  205.         {
  206.             ap_sender = ap_s2tree (contents);
  207.             if (ap_sender == (AP_ptr) NOTOK)
  208.             {
  209.             ll_log (logptr, LLOGFTR, "Illegal sender field: %s",
  210.                     contents);
  211.             return (RP_NO);
  212.             }
  213.             break;      /* now build route */
  214.         }
  215.  
  216.         if (lexequ (name, "From") && !gotfrom)
  217.         {
  218.             if ((ap_sender = ap_s2tree (contents)) !=
  219.                     (AP_ptr) NOTOK)
  220.             gotfrom = TRUE;
  221.         }
  222.         continue;
  223.     }
  224.     break;          /* always fall out of loop */
  225.     }
  226.                 /* build on the route from the trace */
  227.     ap_t2parts (ap_sender, (AP_ptr *) 0, (AP_ptr *) 0,
  228.         &mbox, &domain, &route);
  229.  
  230.     donevia = FALSE;
  231.     FOREVER {
  232.     if (jntroute != (AP_ptr) 0) {
  233.         ap = jntroute;
  234.         jntroute = jntroute -> ap_chain;
  235.     }
  236.     else if (donevia)
  237.         break;
  238.     else {
  239.         donevia = TRUE;
  240.         if (viahost [0] == '\0') {
  241.         char *vh;
  242.         if (curchan->ch_lname) {
  243.             vh = multcat(curchan->ch_lname, ".", curchan->ch_ldomain,
  244.             (char *)0);
  245.             ap = newdomain (vh);
  246.             free(vh);
  247.         }
  248.         else {
  249.             ap = newdomain (
  250.                     isstr(locfullmachine)?locfullmachine:locfullname
  251.             );
  252.         }
  253.         }
  254.         else
  255.         ap = newdomain (viahost);
  256.     }
  257.  
  258.     if (domain == (AP_ptr) 0)
  259.         domain = ap;
  260.     else {
  261.         ap -> ap_chain = route;
  262.         route = ap;
  263.     }
  264.     }
  265.  
  266.                 /* now normalise the domains        */
  267.     if(ap_dmnormalize (domain, (Chan *) NULL) == MAYBE)
  268.     return(RP_NS);
  269.     for (ap = route; ap != (AP_ptr) 0; ap = ap -> ap_chain)
  270.     if(ap_dmnormalize (ap, (Chan *) NULL) == MAYBE){
  271.         ap_free (route);    /* I suspect this is not needed */
  272.         return(RP_NS);
  273.     }
  274.  
  275.                 /* eliminating redundant entries     */
  276.                 /* This made into a loop by Peter C UKC */
  277.                 /* I found that the route could contain */
  278.                 /* several instances of the same string */
  279.                 /* it is prudent to remove them */
  280.     while (route != (AP_ptr) 0)
  281.     {
  282.     for (ap = route; ap -> ap_chain != (AP_ptr) 0; )
  283.         {
  284.         if (ap -> ap_chain ->ap_obtype == APV_NIL)
  285.             break;
  286.         ap = ap -> ap_chain;
  287.     }
  288.  
  289.     if (lexequ (domain -> ap_obvalue, ap -> ap_obvalue))
  290.     {
  291.         ap -> ap_obtype = APV_NIL;
  292.         free (ap -> ap_obvalue);
  293.         ap -> ap_obvalue = (char *) 0;
  294.         if (ap == route)
  295.         {
  296.             ap_free (route);
  297.             route = (AP_ptr) 0;
  298.             break;
  299.         }
  300.     }
  301.         else
  302.         break;
  303.     }
  304.     p = ap_p2s ((AP_ptr) 0, (AP_ptr) 0, mbox, domain, route);
  305.     if(p == (char *)MAYBE)
  306.     return(RP_NS);
  307.     strcpy (sender, p);
  308.     free (p);
  309.     ll_log (logptr, LLOGFTR, "Sender = '%s'", sender);
  310.     return (RP_OK);
  311. }
  312. /*
  313. */
  314. /* basic processing of incoming header lines */
  315. LOCFUN
  316. hdr_parse (src, name, contents)   /* parse one header line              */
  317.     register char *src;           /* a line of header text              */
  318.     char *name,                   /* where to put field's name          */
  319.      *contents;               /* where to put field's contents      */
  320. {
  321.     extern char *compress ();
  322.     char linetype;
  323.     register char *dest;
  324.  
  325. #ifdef DEBUG
  326.     ll_log (logptr, LLOGBTR, "hdr_parse(%s)", src);
  327. #endif
  328.  
  329.     if (isspace (*src))
  330.     {                             /* continuation text                  */
  331. #ifdef DEBUG
  332.     ll_log (logptr, LLOGFTR, "cmpnt more");
  333. #endif
  334.     if (*src == '\n')
  335.         return (HDR_EOH);
  336.     linetype = HDR_MOR;
  337.     }
  338.     else
  339.     {                             /* copy the name                      */
  340.     linetype = HDR_NEW;
  341.     for (dest = name; *dest = *src++; dest++)
  342.     {
  343.         if (*dest == ':')
  344.         break;            /* end of the name                    */
  345.         if (*dest == '\n')
  346.         {                     /* oops, end of the line              */
  347.         *dest = '\0';
  348.         return (HDR_NAM);
  349.         }
  350.     }
  351.     *dest = '\0';
  352.     compress (name, name);    /* strip extra & trailing spaces      */
  353. #ifdef DEBUG
  354.     ll_log (logptr, LLOGFTR, "cmpnt name '%s'", name);
  355. #endif
  356.     }
  357.  
  358.     for (dest = contents; isspace (*src); )
  359.     if (*src++ == '\n')       /* skip leading white space           */
  360.     {                         /* unfulfilled promise, no contents   */
  361.         *dest = '\0';
  362.         return ((linetype == HDR_MOR) ? HDR_EOH : linetype);
  363.     }                          /* hack to fix up illegal spaces      */
  364.  
  365.     while ((*dest = *src) != '\n' && *src != 0)
  366.          src++, dest++;       /* copy contents and then, backup     */
  367.     while (isspace (*--dest));    /*   to eliminate trailing spaces     */
  368.     *++dest = '\0';
  369.  
  370.     return (linetype);
  371. }
  372.